package cn.cvs.service.impl;

import cn.cvs.dao.SysUserMapper;
import cn.cvs.pojo.SysUser;
import cn.cvs.service.SysUserService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

//业务层
@Service
public class SysUserServiceImpl implements SysUserService {

    @Resource
    SysUserMapper sysUserMapper;

    @Resource
    RedisTemplate redisTemplate;

    @Override
    public SysUser queryByAccountAndPassword(String account, String password) {
        SysUser sysUser = sysUserMapper.queryByAccountAndpwd(account, password);
        return sysUser;
    }


    @Override
    public List<SysUser> queryByPage(String realName, Integer roleId, Integer pageBean, Integer pageSize) {
        List<SysUser> sysUsers = sysUserMapper.queryByPage(realName, roleId, (pageBean-1)*pageSize, pageSize);
        return sysUsers;
    }


    @Override
    public int queryByPageCount(String realName, Integer roleId) {
        return sysUserMapper.queryByPageCount(realName, roleId);
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public int addUser(SysUser sysUser) {
        return sysUserMapper.insertSysUser(sysUser);
    }


    //修改的时候要清除缓存
    @Override
    public int updateTrim(SysUser sysUser) {
        //进行修改数据
        int update = sysUserMapper.updateTrim(sysUser);
        //根据所修改的id值获取它的值,如果存在则清除他的缓存
        Object user
                = redisTemplate.opsForValue().get("queryById_" + sysUser.getId());
        if(user!=null){
            //清除缓存
            redisTemplate.delete("queryById_" + sysUser.getId());
        }
        return update;
    }


    //第一次查询的时候
    @Override
    public SysUser queryById(int id) {
        //获取保存在Redis中的值
        Object user = redisTemplate.opsForValue().get("queryById_" + id);
        //如果他不为空
        if(user!=null){
            return (SysUser)user;
        }
        //为空则在数据库查询数据
        SysUser sysUser = sysUserMapper.queryById(id);
        if(sysUser!=null){
            //如果在数据库查询出来的数据不为空,则保存数据
            redisTemplate.opsForValue().set("queryById_" + id, sysUser);
        }
        return sysUser;
    }


    @Override
    public int queryUserExist(String account) {
        return sysUserMapper.queryUserExist(account);
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public int deleteById(Integer id, HttpServletRequest request) {
        SysUser sysUser = sysUserMapper.queryById(id);
        int delete=sysUserMapper.deleteById(id);
        //删除图片
        //身份照
        if (!StringUtils.isEmpty(sysUser.getIdPicPath())) {
            //获得身份照在服务器上的物理地址
            String idPicPath = request.getSession().getServletContext().getRealPath(sysUser.getIdPicPath());
            File idPic = new File(idPicPath);
            if (idPic.exists()) {
                idPic.delete();
            }
        }
        //证件照
        if (!StringUtils.isEmpty(sysUser.getWorkPicPath())) {
            String workPicPath = request.getSession().getServletContext().getRealPath(sysUser.getWorkPicPath());
            File workPic = new File(workPicPath);
            if (workPic.exists()) {
                workPic.delete();
            }
        }
        return delete;
    }
}
